package com.zyh.controller;
import com.zyh.entity.Tag;
import com.zyh.resp.Result;
import com.zyh.service.TagService;
import com.zyh.utils.GetNumber;
import com.zyh.utils.Menu;
import java.util.List;
import java.util.Scanner;
import com.zyh.utils.PermissionUtils;

public class TagController {
    private final TagService tagService = new TagService();
    private final Scanner scanner = new Scanner(System.in);
    private final GetNumber getNumber = new GetNumber();

    public void manageFromUser() {
        manage(true);
    }

    private void manage(boolean isFromUser) {
        while (true) {
            Menu.tagMenu();
            int opt = getNumber.getUserInputAsInt();
            switch (opt) {
                case 1:
                    selectById();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    deleteById();
                    break;
                case 4:
                    selectAll();
                    break;
                case 5:
                    updateById();
                    break;
                case 0:
                    if (isFromUser) {
                        System.out.println("返回用户管理...");
                        return;
                    } else {
                        System.out.println("返回主菜单");
                        return;
                    }
                default:
                    System.out.println("无效选项，请重新选择");
            }
        }
    }

    private void selectById() {
        System.out.print("请输入标签ID：");
        Integer tagId = getNumber.getUserInputAsInt();
        try {
            Result<Tag> result = tagService.selectById(tagId);
            Tag tag = result.getData();
            System.out.println("查询结果：ID=" + tag.getTagId() + "，标签名=" + tag.getName());
        } catch (Exception e) {
            System.out.println("查询失败：" + e.getMessage());
        }
    }

    private void add() {
        if (!PermissionUtils.isAdmin()) {
            System.out.println("操作失败：仅管理员可删除标签");
            return;
        }
        System.out.print("请输入标签名：");
        String name = scanner.nextLine().trim();
        try {
            Result<Void> result = tagService.add(new Tag(name));
            System.out.println(result.getMessage());
        } catch (Exception e) {
            System.out.println("新增失败：" + e.getMessage());
        }
    }

    private void deleteById() {
        if (!PermissionUtils.isAdmin()) {
            System.out.println("操作失败：仅管理员可删除标签");
            return;
        }
        System.out.print("请输入要删除的标签ID：");
        Integer tagId = getNumber.getUserInputAsInt();
        try {
            Result<Void> result = tagService.deleteById(tagId);
            System.out.println(result.getMessage());
        } catch (Exception e) {
            System.out.println("删除失败：" + e.getMessage());
        }
    }

    private void selectAll() {
        try {
            Result<List<Tag>> result = tagService.getAll();
            System.out.println("=====所有标签（共" + result.getData().size() + "条）=====");
            result.getData().forEach(tag ->
                    System.out.println("ID=" + tag.getTagId() + "，标签名=" + tag.getName())
            );
        } catch (Exception e) {
            System.out.println("查询失败：" + e.getMessage());
        }
    }

    private void updateById() {
        if (!PermissionUtils.isAdmin()) {
            System.out.println("操作失败：仅管理员可修改标签");
            return;
        }
        System.out.print("请输入要修改的标签ID：");
        Integer tagId = getNumber.getUserInputAsInt();
        System.out.print("请输入新的标签名：");
        String newName = scanner.nextLine().trim();
        try {
            Result<Void> result = tagService.updateById(tagId, newName);
            System.out.println(result.getMessage());
        } catch (Exception e) {
            System.out.println("修改失败：" + e.getMessage());
        }
    }
}
